package com.liuyanzhao.blog.controller.Admin;


import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.liuyanzhao.blog.entity.Link;
import com.liuyanzhao.blog.entity.custom.LinkCustom;
import com.liuyanzhao.blog.service.LinkService;

/**
 * 
 * @author Lu Han
 *  链接管理  
 * @CrateTime: 2019年6月22日
 */
@Controller
@RequestMapping("/admin/link")
public class BackLinkController {

    @Autowired
    private LinkService linkService;
    /**
     * 
     *   浏览方法
     * @throws Exception
     * @Return: ModelAndView
     * @author Lu Han
     * @CreateDate: 2019年6月22日 下午9:25:36
     */
    @RequestMapping(value = "")
    public ModelAndView linkList() throws Exception {
        ModelAndView modelandview = new ModelAndView();

        List<LinkCustom> linkCustomList = linkService.listLink(null);
        modelandview.addObject("linkCustomList",linkCustomList);

        modelandview.setViewName("Admin/Link/index");
        return modelandview;

    }
    /**
     * 
     *  浏览插入的链接
     * @return
     * @throws Exception
     * @Return: ModelAndView
     * @author Lu Han
     * @CreateDate: 2019年6月22日 下午9:23:08
     */
    @RequestMapping(value = "/insert")
    public ModelAndView insertLinkView() throws Exception {
        ModelAndView modelAndView = new ModelAndView();

        List<LinkCustom> linkCustomList = linkService.listLink(null);
        modelAndView.addObject("linkCustomList",linkCustomList);

        modelAndView.setViewName("Admin/Link/insert");
        return modelAndView;
    }
    /**
     * 
     *  插入链接
     * @param link
     * @return
     * @throws Exception
     * @Return: String
     * @author Lu Han
     * @CreateDate: 2019年6月22日 下午9:21:30
     */
    @RequestMapping(value = "/insertSubmit",method = RequestMethod.POST)
    public String insertLinkSubmit(Link link) throws Exception {
        link.setLinkCreateTime(new Date());
        link.setLinkUpdateTime(new Date());
        link.setLinkStatus(1);
        linkService.insertLink(link);
        return "redirect:/admin/link/insert";
    }

    /**
     * 
     *  删除链接  
     * @param id
     * @return
     * @throws Exception
     * @Return: String
     * @author Lu Han
     * @CreateDate: 2019年6月22日 下午9:26:11
     */
    @RequestMapping(value = "/delete/{id}")
    public String deleteLink(@PathVariable("id") Integer id) throws Exception {

        linkService.deleteLink(id);
        return "redirect:/admin/link";
    }

    /**
     * 
     *   编辑链接的视图
     * @param id
     * @return
     * @throws Exception
     * @Return: ModelAndView
     * @author Lu Han
     * @CreateDate: 2019年6月22日 下午9:26:23
     */
    @RequestMapping(value = "/edit/{id}")
    public ModelAndView editLinkView(@PathVariable("id") Integer id) throws Exception {
        ModelAndView modelAndView = new ModelAndView();

        LinkCustom linkCustom =  linkService.getLinkById(id);
        modelAndView.addObject("linkCustom",linkCustom);

        List<LinkCustom> linkCustomList = linkService.listLink(null);
        modelAndView.addObject("linkCustomList",linkCustomList);

        modelAndView.setViewName("Admin/Link/edit");
        return modelAndView;
    }

    /**
     * 
     *  提交编辑的链接  
     * @param link
     * @return
     * @throws Exception
     * @Return: String
     * @author Lu Han
     * @CreateDate: 2019年6月22日 下午9:26:42
     */
    @RequestMapping(value = "/editSubmit",method = RequestMethod.POST)
    public String editLinkSubmit(Link link) throws Exception {
        link.setLinkUpdateTime(new Date());
        linkService.updateLink(link);
        return "redirect:/admin/link";
    }
}
